package Gather.StreamExercise.StreamDome001;

import java.util.ArrayList;
import java.util.Collections;

/**
 * ClassName:StreamDome007
 * Package:Gather.StreamExercise
 * Description:
 *
 * @Author:ZhangXin
 * @Create2023/2/23-22:40
 * @Version:v1.0
 */
public class StreamDome007 {
    /*
     * Stream<T>filter(Predicate<?super T>predicate  过滤
     * Stream<T>limit(long maxSize)  获取前几个元素
     * Stream<T>skip(long n) 跳过前几个元素
     * */
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list, "张无忌", "周芷若", "赵敏", "张强", "张三丰", "张翠山", "张良", "王二麻子", "谢广坤");
        //Stream<T>filter(Predicate<?super T>predicate  过滤(过滤的规则)
        /*规则：把张开头的留下,其余数据过滤不要*/
//        list.stream().filter(new Predicate<String>() {
//            @Override
//            public boolean test(String s) {
//                //s:依次表示流中的每一个数据
//                //如果返回值为true,表示当前数据留下
//                //如果返回值为false,表示当前数据舍弃不要
//                return s.startsWith("张");
//            }
//        }).forEach(s -> System.out.println(s));

        list.stream().filter(s -> s.startsWith("张")).forEach(s -> System.out.println(s));
        System.out.println(list);


        /*Stream<T>limit(long maxSize)  获取前几个元素*/
        System.out.println("-------获取前几个元素-------");
        list.stream().limit(3).forEach(s -> System.out.println(s));
        /*Stream<T>skip(long n) 跳过前几个元素*/
        System.out.println("-------跳过前几个元素-------");
        list.stream().skip(4).forEach(s -> System.out.println(s));

        //课堂练习
        //"张强", "张三丰", "张翠山"
        System.out.println("\"张强\", \"张三丰\", \"张翠山\"");
        //第一种思路
        //先获取前面6个元素："张无忌", "周芷若", "赵敏", "张强", "张三丰", "张翠山"
        //然后跳过前面3个元素
        list.stream().limit(6).skip(3).forEach(s-> System.out.println(s));

        //第二种思路
        //先跳过3个元素
        //然后在获取前面3个元素
        list.stream().skip(3).limit(3).forEach(s -> System.out.println(s));

    }
}
